// import XLSX from 'xlsx'
import XLSX from 'xlsx'
var ABC=['A','B','C','D','E','F']
const firstHeaderSize=1
function generateMerges(header,bodyLength){ //生成单元格
    let result=[{s:{c: 0,r: 0},e:{c:(header.length-2),r:firstHeaderSize-1}}]
    for(let i=1;i<header.length;i++){
        let item_s={c: i-1,r: firstHeaderSize}
        let item_e={c: i-1,r: firstHeaderSize}
        let item={s:item_s,e:item_e}
        result.push(item)
    }
    let tableHeight=firstHeaderSize+bodyLength
    let item_s={c: 0,r: tableHeight+1}
    let item_e={c: header.length-2,r: tableHeight+1}
    let item={s:item_s,e:item_e}
    result.push(item)
    return result
}
function generateHeader(header){
    let result={}
    result.A1={v:header[0]}
    for(let i=1;i<header.length;i++){
        let key=ABC[i-1]+'2'
        result[key]={v:header[i]}
    }
    return result
}
function generateBody(filterkey,data){
    let result={}
    for(var i=0;i<data.length;i++){
        // result[`A${i+firstHeaderSize+1}`]
        for(var j=0;j<filterkey.length;j++){
            let key=ABC[j]+`${i+firstHeaderSize+2}`
            result[key]={v:data[i][filterkey[j]]}
        }
    }
    let key=ABC[0]+`${i+firstHeaderSize+2}`
    result[key]={v:'汇总（内件数量）：58件',s:{alignment: { horizontal: "center", vertical: "center", wrap_text: true }}}
    return result
}
// 多重表头导出
function mulExportExcel(){
    let dataHeader=['交接清单2131','序号','学院姓名','驾驶证号','出证日期']
    let filterkey=['no','name','driveNumber','createTime']
    let dataBody=[{no:1,name:'pk',driveNumber:'325r23oi59',createTime:'today'},
                    {no:2,name:'pk2',driveNumber:'230883jifjmf',createTime:'today2'},
                    {no:2,name:'pk2',driveNumber:'230883jifjmf',createTime:'today2'},
                    {no:2,name:'pk2',driveNumber:'230883jifjmf',createTime:'today2'},
                    {no:2,name:'pk2',driveNumber:'230883jifjmf',createTime:'today2'},
                    {no:3,name:'pk3',driveNumber:'984sadf948sd',createTime:'today3'},
                    // {All:'汇总（内件数量）：58件'}
                ]
    let header=generateHeader(dataHeader,dataBody.length)
    console.log('header',header)
    let body=generateBody(filterkey,dataBody)
    console.log('body',body)
    let merges=generateMerges(dataHeader,dataBody.length)
    console.log('merges',merges)

    let output=Object.assign({},header,body)
    console.log('output',output)
    let ref = 'A1:Z100'
    var wb = {
        SheetNames: ['mySheet'],
        Sheets: {
          mySheet: Object.assign({}, output, { '!ref': ref, '!merges': merges })
        }
    };
    wb.Sheets.mySheet['A1']={ t: "s", v: '交接清单555',s:{alignment: { horizontal:"center", vertical:"center"}}};
    wb.Sheets.mySheet['A1'].s={
        // font:{
        //     name: '宋体',
        //     sz: 24,
        //     bold: true,
        //     color: { rgb: "FFFFAA00" }
        // },
        alignment: { horizontal: "center", vertical: "center", wrap_text: true },
        fill: { bgcolor: { rgb: 'ffff00' } }
    }
    console.log('wb',wb)
    XLSX.writeFile(wb,'交接清单表.xlsx')
}
export default mulExportExcel
